Awezome

  • 主页
  • 随笔
所有文章 友链 关于我

Awezome

  • 主页
  • 随笔

CSV 在 UTF 8 编码下 Excel 2007 打开乱码的解决方法

2015-03-08

首先说这个问题太变态了,Office自己都和自己不兼容,正常的utf8编码下 07 局然打开中文乱乱码。当然首选想到是转成gbk这样中文系编码,这样中文没有了问题,但是对于其它小语种也是有同样乱码的问题。最后的最后还是Google到了解决方法。

先下这个网页,CSV tests encoding and column separator
上面对比测试了csv不同编码不同头不同分隔符在 excel 2003 和 excel 2007 乱码情况,也就是说在unicode系下,要保证没有乱码要做到3点:utf16le编码,tab分隔,加bom 。

下面是PHP版的最终代码,参考这里 excel mangles diacritics in csv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Export an array as downladable Excel CSV
* @param array $header
* @param array $data
* @param string $filename
*/
function toCSV($header, $data, $filename) {
$sep = "\t";
$eol = "\n";
$csv = count($header) ? '"'. implode('"'.$sep.'"', $header).'"'.$eol : '';
foreach($data as $line) {
$csv .= '"'. implode('"'.$sep.'"', $line).'"'.$eol;
}
$encoded_csv = mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '. strlen($encoded_csv));
echo chr(255) . chr(254) . $encoded_csv;
exit;
}
赏

谢谢你请我吃糖果

  • PHP

扫一扫,分享到微信

MySQL复习笔记:索引
快速幂取模算法 A^B Mod C
© 2014-2019 Awezome
Hexo Theme Zilia by Awezome